package com.webserver.core;

import com.webserver.http.HttpRequest;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**处理与指定客户端的HTTP交互
 * HTTP
 *
 */

public class ClientHandler implements Runnable {
    private Socket socket;

    public ClientHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //1 解析请求
            HttpRequest request = new HttpRequest(socket);
            //2 处理请求

            //3 放送响应

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //交互完毕后与客户端断开
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }




    private String readLine() throws IOException{
        InputStream in = socket.getInputStream();
        StringBuilder builder = new StringBuilder();
        int d;
        char pre = 'a',cur = 'a';
        while((d=in.read())!=-1){
            cur = (char)d;
            if (pre==13 && cur == 'a'){
                break;
            }
            builder.append(cur);
            pre = cur;
        }
        return  builder.toString().trim();
    }
}
            /*StringBuilder builder1 =new StringBuilder();
            char b;
            for (int i=0;i<line.length();i++){
                b=line.charAt(i);
                line.substring(i);
                if (b=='/'){
                    break;
                }
                method+=b;
            }
            for (int i=0;i<line.length();i++){
                b=line.charAt(i);
                line.substring(i);
                if (b=='/'){
                    break;
                }
                uri+=b;


            }*/